Repository navigation
feat(agent): lk agent create and deploy from source through the Public API when signed in - #1141
Topherhindman wants to merge 12 commits into
Conversation
b6eb95f to
1406521
Compare
1406521 to
1af7fad
Compare
1af7fad to
24a0b1c
Compare
CreateAgent registers an agent in a region, DeployAgent returns the tag of the version and the presigned POST to upload the source to, and StartAgentBuild starts the build of what was uploaded. The build runs on after StartAgentBuild returns; callers poll GetAgent and ListAgentVersions for its end.
Under --user-auth, lk agent create, lk agent deploy and lk agent init --deploy no longer go through the token bridge, which needs a project admin. CreateAgent registers the agent in its region, DeployAgent returns the tag of the version and where to upload the source, lk uploads the same tarball it uploads with an API key, and StartAgentBuild starts the build. A WRITE member may do all of it; secrets still need a project admin, and the refusal says that a writer may deploy without them. The Public API doesn't stream the build, so lk polls the agent and its versions every 5 seconds, prints each new status of the deployment, and prints the version's build log once the build ends. lk's build timeout and Ctrl-C keep their meaning: the build continues on LiveKit Cloud. An interrupt before the build starts says that nothing was deployed.
fakeBuild recorded only each request's method and path, so the tests passed with create's secrets or region, deploy's deployment or secrets, or the build's deployment or attributes dropped. It now records each URL's query and each JSON body, and the tests assert them. The create/deploy script passes secrets, attributes and a deployment under --user-auth and prints the bodies with `requests -v`.
… behind "nothing was deployed" wasn't so: before the build starts, DeployAgent has already set the secrets the deploy sent, and cloud agents shows the production deployment as Building. The message now says that no version was built or deployed, that the secrets are set, and that the status may show Building.
…siently One failed read of the agent or its versions, such as a 503, a network error or a rate limit, ended the deploy with "can't follow the build". awaitBuild now makes such a read again, up to pollAttempts times with a doubling backoff, before it gives up. A 401, a 403 or any other 4xx other than 429 fails at once, as before.
…out its version awaitBuild stopped only when the version was Available or Failed, or the deployment showed Build Failed. A build that ended without listing its version, such as one a newer deploy replaced or one the builder dropped, was polled until the 15-minute timeout, which then blamed a large image. Once the deployment has left Setting Up, Building and Scheduling, and the version is still not listed buildEndGrace (a minute) later, awaitBuild now says that the build ended without the version, and points at `lk agent versions` and the build log.
…r step fails Under --user-auth, create registers the agent, then uploads and builds its source. When anything failed after the agent was registered (the upload, a build that didn't start or failed, Ctrl-C, the timeout), lk didn't save livekit.toml, and on a timeout or Ctrl-C its message dropped the agent's ID. lk now saves the ID and says "Created agent with ID", as the --image path does once it registers the agent, so that `lk agent deploy` and `lk agent status` find it; Ctrl-C points at `lk agent status`. With an API key, cloud agents' CreateAgent registers and builds in one call, and nothing changes.
… warning After create succeeded, answering yes to "view logs?" as a member below admin got the token bridge's refusal and exit status 1, though the agent was created. lk can't tell the member's role without asking, so it now warns that runtime logs need a project admin and exits 0. lk agent logs still fails on the same refusal.
…fuses The Public API no longer refuses a LIVEKIT_ name (livekit/public-api-server#242), so TestUserAuthDeployAgentRefusals renders a name that it refuses: one that starts with a digit.
…'s cloud public.Agent's Info is Cloud since the Public API moved it (livekit/public-api-server#240), so awaitBuild reads the deployment's status in Cloud's deployments and returns Cloud once the build succeeds. The deploy tests and the create/deploy script answer GetAgent in the new shape.
… deploy As for the other writes, a deploy that AgentService refuses because the agent is an Agent Builder agent now names the lk command to use, with the Agent Builder agent's ID from the refusal's ResourceInfo detail: lk agent builder deploy AB_ID, in place of the dashboard.
TestUserAuthDeployAgentTimeout failed on CI's macOS runner: its 50ms deadline passed before StartAgentBuild, so the deploy stopped as one interrupted before its build started, which doesn't wrap the deadline's error. The last case of TestUserAuthDeployAgentBuildEndsWithoutVersion had the same race. Both now use a context that ends as a passed deadline does once the fake has answered a number of version polls, so the build has always started, however slow the runner.
24a0b1c to
f9ad76e
Compare
There was a problem hiding this comment.
Devin Review found 1 potential issue.
2 flags not posted on this PR by your GitHub settings — view them in Devin Review. (Configure)
| d, err := a.client.DeployAgent(ctx, a.projectID, agentID, deployment, secrets) | ||
| if err != nil { | ||
| return nil, deployError(err, agentID, secrets) |
There was a problem hiding this comment.
🟡 Canceled deployment reports false success
If Ctrl-C interrupts DeployAgent before upload, the CLI exits successfully and says the build continues. It never calls StartAgentBuild, so no build runs.
Learn more
The signed-in create and deploy commands first prepare a deployment, then upload source, then explicitly queue the build. If Ctrl-C cancels the preparation request, deployError and agentServiceError preserve the cancellation. Both createAgent and deployAgent interpret that cancellation as a detached build and return success. The explicit interruption check only runs after upload, so it cannot catch this case.
Example: Run lk --user-auth agent deploy and press Ctrl-C during the Public API preparation request. The CLI says the deploy continues, but it has not uploaded the source or asked the server to build it.
Recommended fix: Give a confirmed queued build that the CLI stops following a distinct error type and use it for the signed-in success-on-disconnect path. Keep the registered agent ID on partial create, preserve the legacy client behavior, and test cancellation during preparation separately from cancellation during polling.
Was this helpful? React with 👍 or 👎 to provide feedback.
Under
--user-auth,lk agent create,lk agent deployandlk agent init --deploy(from source) now go through the Public API'sAgentServicein place of the token bridge. The bridge needsAgent.Admin, so before this only a project admin could create or deploy an agent. Now a WRITE member can do both. Secrets still need a project admin, and the refusal says that a writer can deploy without them.Stacked on #1140.
How a deploy runs
CreateAgent(create only) registers the agent in the region that lk resolved (--regionor the picker, as today).DeployAgentsets the secrets and returns the version's tag and a presigned POST.cloudagents.CreateSourceTarballwith lk's excludes) and uploads it withcloudagents.MultipartUpload.StartAgentBuildstarts the build and returns when the build is queued.GetAgentandListAgentVersionsevery 5 seconds. It prints each new status of the target deployment. When the version with the tag isAvailableorFailed, or the deployment isBuild Failed, it prints the version's build log fromGetAgentBuildLogand stops.Commands after this PR
--user-authagent create(source),agent init --deployCreateAgent,DeployAgent, upload,StartAgentBuild, pollagent deploy(source)DeployAgent, upload,StartAgentBuild, pollagent list,status,config,versions,dockerfile, buildlogsAgentService(#1138, #1139)agent secretsAgentService(#1139)agent restart,rollback,promoteAgentService(#1140)agent delete,update-secrets,updatewith secretsAgentService(#1140)agent create --image/--image-tar,deploy --image/--image-tarlogs,update-secrets --overwriteagent private-link *What's in it
feat(public):CreateAgent,DeployAgent(returns aSourceDeploy: tag, upload URL, form fields) andStartAgentBuild.feat(agent): nativeCreateAgentandDeployAgentV2inuserAuthAgentAPI, with the poll and the error mapping.fix(agent): the poll reads the deployment's status in the agent'scloud.agentDeployments, which replacedinfoin livekit/public-api-server#240.Decisions
StartAgentBuildhas returned. The poll reads the deployment's row inGetAgent(the status of the target deployment, not of production) and the version inListAgentVersions. A poll that fails transiently (429, 5xx, no answer) is made again up to 4 times with a doubling backoff from 1 second; when it still fails, or fails another way, lk says that the build continues and to runlk agent status. When the deployment has leftSetting Up,BuildingandSchedulingand the version is still not listed a minute later, lk says that the build ended without the version and points atlk agent versionsand the build log.StartAgentBuildreturns "interrupted before the build started: no version was built or deployed, but the secrets this deploy sent are set, and the agent's status may show Building". lk would otherwise read it as a disconnect and say that the build continues. (With an API key, the SDK has the same gap.)livekit.toml, prints "Created agent with ID [CA_…]", as the--imagepath does once it registers the agent, and the error says "created agent CA_…, but …".lk agent deployandstatusthen find the agent. With an API key, cloud agents'CreateAgentregisters and builds in one call, so nothing changes there. A rerun oflk agent createstill creates another agent in both modes, as before.CreateAgent, and deploy sends them onDeployAgent. When a member below admin sends secrets, lk says: "this command needs a project admin. Write access to the project is enough without secrets (from --secrets, --secrets-file, --secret-mount or an env file): a project admin can set them withlk agent update-secrets. …". Invalid secrets get a line each, by name, as in feat(agent): lk agent restart, rollback, promote, delete and secrets through the Public API when signed in #1140.lk agent builder deploy AB_…, with theAB_ID from the refusal'sResourceInfodetail. A hint for a writer who needs an admin says a project API key also works: "pass --legacy-auth", as chore(auth): prepare for migration to user-auth as default #1156's hints do.--regionflag, the picker, or a non-interactive error.userAuthAgentAPI.CreateAgentrefuses any other count.GetAgent, so the prompt works for an admin. A writer gets a warning that runtime logs need a project admin, and create exits 0.Output differences vs the API-key path
Status: <status>line on each change, and the full build log, as plain text, at the end.BUILD ERROR:line.Created agent with ID,Build completed …,Completed deployment of agent … to production, links) are the same. The testscript compares them.Open questions
--user-authwait longer, since it costs only polls? This PR keeps 15 minutes.lk agent createreads.env/.env.localby default, so a writer whose env file has any secret other thanLIVEKIT_URL/API_KEY/API_SECRETis refused. They can pick[none]in the picker, or remove the file. Should lk skip the env file under--user-authfor a non-admin, or ask?MultipartUploadtakes no context (Upload cloudagents source with the caller's context and HTTP client server-sdk-go#1096 adds one). A Ctrl-C during a large upload waits for the upload to end.StartAgentBuildnever happens, cloud agents keeps the deploymentBuilding(Agent Hosting question in livekit/public-api-server#244).AvailableandFailedare cloud agents' strings, which the Public API passes through. Should the API give them an enum?Blockers
pkg/publicgenerated from livekit/public-api-server@372fb27e (production v2026-12-02-2258).Testing
GOPRIVATE= go build ./...,go vet ./cmd/lk/... ./pkg/...,go test ./cmd/lk/... ./pkg/...andgolangci-lint run ./cmd/lk/... ./pkg/public/...pass.agent-create-deploy-with-user-auth:createanddeployfrom a Python project, once with an API key (fake cloud agents, fake presigned upload target, empty build stream) and once under--user-auth(fake Public API, same upload target). Both outputs are compared in full, and so is the request log, which has no token request. Also covers a failed build that prints its log.agent_user_auth_deploy_test.go) against a fake Public API that also serves the presigned POST:livekit.toml,.env*ornode_modules), the status lines, the build log, and the server regions;Build Failed;DeadlineExceeded;pkg/publictests for the three calls' requests and bodies.Review fixes
fakeBuildrecords each URL's query and each JSON body; the unit tests assert create's region and secrets, deploy's deployment and secrets, and the build's tag, deployment and attributes. The create/deploy script runs--user-authwith--secrets,--attributeand--deployment stagingand prints the bodies withrequests -v(secret values redacted). Dropping any of them fails a test (checked by mutation).LIVEKIT_change.TestUserAuthDeployAgentRefusalsnow renders a secret name that the Public API still refuses (beacac2): it no longer refuses aLIVEKIT_name (livekit/public-api-server#242).go build,go vet,go test ./cmd/lk/... ./pkg/...andgolangci-lintpass.StartAgentBuildon a slow runner. They now end the deadline after a set number of polls; they pass 10 times under-race, andtest (macos-latest)passes.--user-authstill says "build timed out possibly due to large image size", as with an API key, though the build continues on LiveKit Cloud.Demo
Recorded on staging, not uploaded yet:
~/hosted-agents-work/demos/M4/m4-deploy-from-source.mp4(write-up~/hosted-agents-work/demos/M4/README.md). Local Public API from this stack against staging cloud-agents (staging database read-only); lk from #1142. READ is refused a create; WRITE creates an agent from source and deploys a new version (real builds, about two minutes each); WRITE with a secret in.env.localis refused.Depends on #1136 (the hints name
lk agent builder).